# pylint: disable-msg=W0232
"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey, UniqueConstraint
from sqlalchemy.types import String, Boolean, Integer, Text, DateTime, Enum
from sqlalchemy.orm import relation

from pylons.i18n import lazy_ugettext as _

from chowa.model.meta import Base
from chowa.model.auth import User

__all__ = ['App', 'Option', 'Error', 'Page',
           'Message', 'Receipt', 'Blog', 'BlogPost']


class App(Base):
    __tablename__ = 'site_app'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(32), unique=True)
    url_prefix = Column(String(255), unique=True)
    enabled = Column(Boolean, default=True)
    
    def __unicode__(self):
        return self.name


class Message(Base):
    __tablename__ = 'site_message'
    
    id = Column(Integer, primary_key=True)
    text = Column(Text)
    sender_id = Column(Integer, ForeignKey(User.id))
    created = Column(DateTime)
    
    sender = relation(User)
    
    def __unicode__(self):
        return self.text
    

class Receipt(Base):
    __tablename__ = 'site_message_receipt'
    
    id = Column(Integer, primary_key=True)
    message_id = Column(Integer, ForeignKey(Message.id))
    recipient_id = Column(Integer, ForeignKey(User.id))
    is_received = Column(Boolean, default=False)
    is_read = Column(Boolean, default=False)
    received = Column(DateTime)
    
    message = relation(Message, backref='recipients')
    recipient = relation(User, backref='messages')
    
    def __unicode__(self):
        return self.message


class Blog(Base):
    __tablename__ = 'site_blog'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), unique=True)
    user_id = Column(Integer, ForeignKey(User.id))
    
    user = relation(User)
    
    def __unicode__(self):
        return self.name


class BlogPost(Base):
    __tablename__ = 'site_blog_post'
    
    id = Column(Integer, primary_key=True)
    title = Column(Text, info={'verbose': _('Title')})
    text = Column(Text, info={'verbose': _('Text')})
    created = Column(DateTime)
    modified = Column(DateTime)
    blog_id = Column(Integer, ForeignKey(Blog.id))
    
    blog = relation(Blog)
    
    def __unicode__(self):
        return self.title


class Option(Base):
    __tablename__ = 'site_option'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(255), unique=True)
    value = Column(Text)
    default = Column(Text)
    type = Column(Enum('text', 'integer', 'float', 'boolean',
                       'month', 'group'))
    verbose = Column(String(255))
    app_id = Column(Integer, ForeignKey(App.id))
    
    app = relation(App)


class UserOption(Base):
    __tablename__ = 'site_user_option'
    __table_args__ = (UniqueConstraint('option_id', 'user_id'),)
    
    id = Column(Integer, primary_key=True)
    option_id = Column(Integer, ForeignKey(Option.id))
    user_id = Column(Integer, ForeignKey(User.id))
    value = Column(Text)
    
    option = relation(Option)
    user = relation(User)
    

class Error(Base):
    __tablename__ = 'site_error'
    
    id = Column(Integer, primary_key=True)
    code = Column(Integer)
    url = Column(String(255))
    referer = Column(String(255))
    happened = Column(DateTime)
    user_id = Column(Integer, ForeignKey(User.id))
    
    user = relation(User)


class Page(Base):
    __tablename__ = 'site_page'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), unique=True)
    title = Column(String(255))
    content = Column(Text)
    modified = Column(DateTime)
    